<?php
// $HeadURL: https://joomgallery.org/svn/joomgallery/JG-1.5/JG/branches/administrator/components/com_joomgallery/model.php $
// $Id: model.php 1930 2010-03-06 12:25:59Z mab $
/****************************************************************************************\
**   JoomGallery  1.5.5                                                                 **
**   By: JoomGallery::ProjectTeam                                                       **
**   Copyright (C) 2008 - 2010  JoomGallery::ProjectTeam                                **
**   Based on: JoomGallery 1.0.0 by JoomGallery::ProjectTeam                            **
**   Released under GNU GPL Public License                                              **
**   License: http://www.gnu.org/copyleft/gpl.html or have a look                       **
**   at administrator/components/com_joomgallery/LICENSE.TXT                            **
\****************************************************************************************/

defined('_JEXEC') or die('Direct Access to this location is not allowed.');

jimport( 'joomla.application.component.model');

/**
 * JoomGallery Parent Model
 *
 * @package JoomGallery
 * @since   1.5.5
 */
class CongregationModel extends JModel
{
	/**
	* JApplication object
	*
	* @access  protected
	* @var     object
	*/
	var $_mainframe;

	//var $_db;
	/**
	* JoomConfig object
	*
	* @access  protected
	* @var     object
	*/
	var $_config;

	/**
	* JoomAmbit object
	*
	* @access  protected
	* @var     object
	*/
	var $_ambit;

	/**
	* JUser object, holds the current user data
	*
	* @access  protected
	* @var     object
	*/
	var $_user;

	/**
	* Constructor
	*
	* @access  protected
	* @return  void
	* @since   1.5.5
	*/
	function __construct($config = array())
	{
		parent::__construct($config);

		$this->_mainframe = & JFactory::getApplication();
		$this->_user      = & JFactory::getUser();
		//$this->_db	=& JFactory::getDBO();

	}
	
	function getHouse($params = array())
	{
		$default_params = array(
		'having' => array()
		);
		$secured_params = array();
		foreach($default_params as $key => $val)
		{
			if(!isset($params[$key]))
			{
				$secured_params[$key] = $val;
			}
			else
			{
				$secured_params[$key] = $params[$key];
			}
		}
		
		if(empty($this->_datavalues[md5(serialize($secured_params))]))
		{
			$query = "SELECT c.*, p.p_id, p.p_name, count(m.username) AS members 
				FROM #__congregation_casa AS c 
				LEFT JOIN #__congregation_profile AS m
					on c.c_id=m.c_id 
				LEFT JOIN #__congregation_province AS p
					on c.p_id=p.p_id
				GROUP BY c.c_id " .
				(count($secured_params['having']) ? 'HAVING ' . implode(' AND ', $secured_params['having']) : '');
		
				$this->_db->setQuery( $query );
				$this->_datavalues[md5(serialize($secured_params))] = $this->_db->loadObject();
				
		}
		return $this->_datavalues[md5(serialize($secured_params))];
	}
	
	/*
	function getHouseList($params = array())
	{
	*/
	function getHouseList()
	{
		$where	= JRequest::getVar('where'); 
		
		$where = count($where) ? 'HAVING ' . implode(' AND ', $where) : '';
		
		$query = "SELECT c.*, p.p_id, p.p_name, count(t.t_username) AS members 
			FROM #__congregation_casa AS c 
			LEFT JOIN #__congregation_profile_paths AS t
				on (c.c_id=t.t_cid AND t.t_current = 1)
			LEFT JOIN #__congregation_province AS p
				on c.p_id=p.p_id
			GROUP BY c.c_id " .
			$where . 
			" ORDER BY p.ordering, c.ordering";
			
	//echo $query;
			$this->_db->setQuery( $query );
		return $this->_db->loadObjectList();
	
	}

	function getHouseCount()
	{
	
		$where	= JRequest::getVar('where'); 
		
		$where = count($where) ? 'HAVING ' . implode(' AND ', $where) : '';
		
		$query = "SELECT c.*, p.p_id, p.p_name, count(m.username) AS members 
			FROM #__congregation_casa AS c 
			LEFT JOIN #__congregation_profile AS m
				on c.c_id=m.c_id 
			LEFT JOIN #__congregation_province AS p
				on c.p_id=p.p_id
			GROUP BY c.c_id " .
			$where;
	
			$this->_db->setQuery( $query );
		return count($this->_db->loadObjectList());
	}
		
	
	function getProvince()
	{
		$default_params = array(
		'having' => array(),
		'where' => array()
		);
		$secured_params = array();
		foreach($default_params as $key => $val)
		{
			if(!isset($params[$key]))
			{
				$secured_params[$key] = $val;
			}
			else
			{
				$secured_params[$key] = $params[$key];
			}
		}
		
		if(empty($this->_datavalues[md5(serialize($secured_params))]))
		{
			$query = "SELECT m.* 
				FROM #__congregation_profile AS m " .
				(count($secured_params['having']) ? 'WHERE ' . implode(' AND ', $secured_params['where']) : '');
		
				$this->_db->setQuery( $query );
				$this->_datavalues[md5(serialize($secured_params))] = $this->_db->loadObject();
				
		}
		
		return count($this->_datavalues[md5(serialize($secured_params))]);
	}
	
	function getProvinceList()
	{
	
	}
	
	function getProvinceCount()
	{
	
	}
	
	function getStatus()
	{
	
	}
	
	function getStatusList()
	{
		$query = "SELECT * FROM #__congregation_status ORDER BY ordering";
		$this->_db->setQuery($query);
		return $this->_db->loadObjectList();
		
	
	}
	
	function getCharge()
	{
	
	}
	
	function getChargeList()
	{
		$query = "SELECT * FROM #__congregation_charge ORDER BY ordering";
		$this->_db->setQuery($query);
		return $this->_db->loadObjectList();
	
	}
	
	function getProfile()
	{
	
	}
	
	function getProfileList()
	{
		$where	= JRequest::getVar('where'); 
		
		$where = count($where) ? ' WHERE ' . implode(' AND ', $where) : '';
		
		/*
		$query = "SELECT m.*, u.name, u.email, r.charge, s.abbr FROM #__congregation_profile AS m " .
		" LEFT JOIN #__users AS u ON m.username=u.username " .
		" LEFT JOIN #__congregation_charge AS r ON m.charge=r.id " .
		" LEFT JOIN #__congregation_status AS s ON m.status=s.id " .
		
		*/
		
		$query = "SELECT m.*, t.t_charge, u.name, u.email, s.abbr FROM #__congregation_profile AS m " .
		" LEFT JOIN #__users AS u ON m.username=u.username " .
		" LEFT JOIN #__congregation_profile_paths AS t ON (t.t_username = m.username AND t.t_current= 1) " .
		" LEFT JOIN #__congregation_casa AS c ON t.t_cid=c.c_id " . 
		//" LEFT JOIN #__congregation_charge AS r ON m.charge=r.id " .
		" LEFT JOIN #__congregation_status AS s ON m.status=s.id " .

			$where;
		
		$query .= " ORDER BY m.ordering, m.lname";
		
			$r = $this->_db->setQuery( $query );
			
		return $this->_db->loadObjectList();
	}
	
	function getProfileCount()
	{
	
	}
	
	function getUsers($params = array())
	{
		$default_params = array(
		'where' => array()
		);
		$secured_params = array();
		foreach($default_params as $key => $val)
		{
			if(!isset($params[$key]))
			{
				$secured_params[$key] = $val;
			}
			else
			{
				$secured_params[$key] = $params[$key];
			}
		}
		if(empty($this->_datavalues[md5(serialize($secured_params))]))
		{
			$query = "SELECT * FROM #__users " .
				(count($secured_params['where']) ? 'WHERE ' . implode(' AND ', $secured_params['where']) : '');
		
				$this->_db->setQuery($query);
				$this->_datavalues[md5(serialize($secured_params))] = $this->_db->loadObjectList();
				
		}
		return $this->_datavalues[md5(serialize($secured_params))];
	
	}
	
	function getUser($params)
	{
		$default_params = array(
		'where' => array()
		);
		$secured_params = array();
		foreach($default_params as $key => $val)
		{
			if(!isset($params[$key]))
			{
				$secured_params[$key] = $val;
			}
			else
			{
				$secured_params[$key] = $params[$key];
			}
		}
		
		if(empty($this->_datavalues[md5(serialize($secured_params))]))
		{
		
			$query = "SELECT * FROM #__users " .
				(count($secured_params['where']) ? 'WHERE ' . implode(' AND ', $secured_params['where']) : '');
		
				$this->_db->setQuery( $query );
				$this->_datavalues[md5(serialize($secured_params))] = $this->_db->loadObject();
				
		}
		return $this->_datavalues[md5(serialize($secured_params))];
	}
}